package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.domain.Personal;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.IPersonalRedisService;
import cn.wolfcode.wolf2w.repository.PersonalRepository;
import cn.wolfcode.wolf2w.service.IPersonalService;
import cn.wolfcode.wolf2w.service.impl.PersonalServiceImpl;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.RequestWrapper;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("personal")
public class PersonalController {


    @Autowired
    PersonalRepository personalRepository;
    @Autowired
    IPersonalService personalService;
    @Autowired
    IPersonalRedisService personalRedisService;

    //开始前端的展示逻辑
    //首先当前端被访问的时候加载相关数据
    @GetMapping("getVisitors")
    public Object getVisitors(String uid){
        return JsonResult.success(personalService.getVisitors(uid));
    }
    //直接访问某用户的
    @GetMapping("visit")
    public Object visit(String vid,String uid){
        //首先获取判断访问者是不是本用户，如果是则不作操作  如果不是则所有地方加1
//        System.out.println(vid);
        if(vid != uid){
            //mongo 列表插入一个vid
            personalService.visit(uid,vid);
            //redis 的两个字段分别加1
            personalRedisService.AllVistitorAdd(uid,vid);
            personalRedisService.OneDayVistitorAdd(uid,vid);
        }
        return JsonResult.success();
    }

    //直接访问某用户的
    @GetMapping("allVisitors")
    public Object allVisitors(String vid,String uid){
        //首先获取判断访问者是不是本用户，如果是本人则渲染该页面 否则表示用户不正确
//        if(vid != uid){
//            //则返回用户错误
//            return JsonResult.defaultError();
//        }
        return JsonResult.success(personalService.getAllVisitors(uid));
    }
    //直接访问某用户的
    @GetMapping("totalView")
    public Object totalView(String vid,String uid){
        //如果存在可以  返回  不存在返回0

        return JsonResult.success(personalRedisService.getAllDayTimes(uid,vid));
    }

    //直接访问某用户的
    @GetMapping("todayView")
    public Object todayView(String vid,String uid){

        return JsonResult.success(personalRedisService.getOneDayVistitor(uid,vid));
    }

    //初始化数据库数据  用于测试
    @GetMapping("dataInit")
    public Object dataInit(){
        Personal p1 = new Personal();
        p1.setUserId("5e295f01a00e265228f963ea");
        List<String> vistsId = new ArrayList<>();
        vistsId.add("5e92d6cdacd8de311e99c99e");
        vistsId.add("5f0dc0752f1e167b25d618f8");
        p1.setVisitorIds(vistsId);
        personalRepository.save(p1);
        Personal p2 = new Personal();
        p2.setUserId("5e92d6cdacd8de311e99c99e");
        List<String> vistsId2 = new ArrayList<>();
        vistsId2.add("5e295f01a00e265228f963ea");
        vistsId2.add("5f0dc0752f1e167b25d618f8");
        p2.setVisitorIds(vistsId2);
        personalRepository.save(p2);
        Personal p3 = new Personal();
        p3.setUserId("5f0dc0752f1e167b25d618f8");
        List<String> vistsId3 = new ArrayList<>();
        vistsId3.add("5e295f01a00e265228f963ea");
        vistsId3.add("5e92d6cdacd8de311e99c99e");
        p3.setVisitorIds(vistsId3);
        personalRepository.save(p3);
        return p3;
    }
}
